//
//  BaseVC.m
//  echo
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Danny. All rights reserved.
//

#import "BaseVC.h"
#import "NetworkingHandler.h"
#import "ModelForScrollView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <DKNightVersion.h>
@interface BaseVC ()<UIScrollViewDelegate, UIPageViewControllerDelegate>

@end

@implementation BaseVC

- (void)dealloc{
    [_backView release];
    [_pageView release];
    [_arr release];
    [_timer release];
    self.scrollView.delegate = nil;
    [_scrollView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleDataForScrollViewImage];
}

- (void)handleDataForScrollViewImage{
    [NetworkingHandler networkingGetWithUrl:@"http://echosystem.kibey.com/index/banner?position=8" dicForValue:@{@"User-Agent":@"echo ios 1.0 20151114;(iPhone,iPhone OS8.3);IDFA F37139C2-85EF-45C8-B64D-2D16D2772A96 Paros/3.2.13"}  completeHandle:^(id result) {

            NSArray *arr = [result objectForKey:@"result"];
            self.arr = [NSMutableArray array];
            for (NSDictionary *dic in arr) {
                ModelForScrollView *model = [[ModelForScrollView alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arr addObject:model];
                
                [model release];
            }
        [self.arr addObject:[self.arr firstObject]];
        [self.arr insertObject:[self.arr objectAtIndex:self.arr.count - 2] atIndex:0];

        [self createScrollView];
    }];
    
}

#pragma mark 子类一定要调用这个方法!!!
- (void)createScrollView{

#pragma mark 子类一定要重写这句消息!!!
//    [self.view addSubview:self.backView];
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 200 * HEIGHT)];
    self.backView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(6 * WIDTH, 8 * HEIGHT, self.view.frame.size.width - 12 * WIDTH, 184 * HEIGHT)];
    [self.backView addSubview:self.scrollView];
    [_scrollView release];

    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.arr.count, 184 * HEIGHT);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;

    for (int i = 0; i < self.arr.count; i ++) {
        ModelForScrollView *model = [self.arr objectAtIndex:i];

        UIImageView *imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
        imageView.frame = CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        [self.scrollView addSubview:imageView];
        [imageView release];
    }

    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
   [self createPageView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.scrollView.contentOffset.x >= (NSInteger)self.scrollView.frame.size.width * (self.arr.count - 1)) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
    }else if (self.scrollView.contentOffset.x == 0){
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.arr.count - 2), 0)];
    }
    
    if ((NSInteger)_scrollView.contentOffset.x / (NSInteger)_scrollView.frame.size.width == 0 || (NSInteger)_scrollView.frame.size.width == 0) {
        _pageView.currentPage = 0;
    }else {
        
        _pageView.currentPage = ((NSInteger)_scrollView.contentOffset.x / (NSInteger)_scrollView.frame.size.width - 1);
    }
}

- (void)createPageView{
    
    self.pageView = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 2, 40 * HEIGHT)];
    self.pageView.center = CGPointMake(self.view.frame.size.width / 2, 173 * HEIGHT);
    [self.backView addSubview:self.pageView];
    [_pageView release];
    self.pageView.enabled = YES;
    self.pageView.numberOfPages = self.arr.count - 2;
    
    [self.pageView addTarget:self action:@selector(pageChange) forControlEvents:UIControlEventValueChanged];
    [self startAction];
}

- (void)pageChange{
    
    NSInteger num = self.pageView.currentPage;

    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (num + 1), 0) animated:YES];
    
}

-(void)startAction{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
    }
    
}

-(void)changePage{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0);
        
    } completion:^(BOOL finished) {
        
        [self scrollViewDidEndDecelerating:self.scrollView];
        
        finished = YES;
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
